//
//  MenuTB.m
//  K-Music
//
//  Created by CHUN on 15/12/27.
//  Copyright © 2015年 CHUN. All rights reserved.
//

#import "MenuTB.h"
#import "MovingVC.h"
#import "RushVC.h"
#import "MaiVC.h"
#import "Chat.h"
#import "MineVC.h"

#import "LoginVC.h"

#import "PSNavigationVC.h"
#import "PSTabBar.h"

@interface MenuTB ()

@property (nonatomic,weak)MovingVC * home;
@property (nonatomic,weak)RushVC   * message;
@property (nonatomic,weak)MaiVC *  mai;
@property (nonatomic,weak)Chat * discover;
@property (nonatomic,weak)MineVC * profile;

@end

@implementation MenuTB

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addAllChildVCs];
    
    [self addCustomTabBar];
    
    //预览模式
    self.selectedIndex = 1;

}

- (void)addCustomTabBar
{
    // 创建自定义tabbar
    __weak typeof(self) ws = self;
    PSTabBar * customTabBar = [[PSTabBar alloc] init];
    customTabBar.delegate = self;

    customTabBar.block = ^()
    {
        ws.selectedIndex = 2;
    };
    [self setValue:customTabBar forKeyPath:@"tabBar"];
    self.tabBar.backgroundColor=[UIColor whiteColor];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
    NSLog(@"%ld",(long)item.tag);
    NSString *str = [FileStorage NSUserDefault_getObjectForKey:access_token];
    if (item.tag == 3 || item.tag == 0 ||item.tag == 4) {

        if (strIsEmpty(str)) {
            
            LoginVC * login  = [[LoginVC alloc] init];
            UINavigationController* navi = [[UINavigationController alloc]initWithRootViewController:login];
            [self presentViewController:navi animated:YES completion:^{
                
                self.selectedIndex = 1;

            }];

        }
    }
    
}

- (void)addAllChildVCs
{
    // 3.设置子控制器
    MovingVC * home = [[MovingVC alloc] init];
    self.home = home;
    [self addChildVc:self.home title:@"互动" image:@"Moving1" selectImage:@"Moving2" tag:0];
    
    RushVC *message = [[RushVC alloc] init];
    self.message = message ;
    [self addChildVc:self.message title:@"冲榜" image:@"Rush1" selectImage:@"Rush2" tag:1];
    
    MaiVC *  mai = [[MaiVC alloc] init];
    self.mai = mai;
    [self addChildVc:self.mai title:@"麦霸" image:@"mai@2x_92" selectImage:@"mai@2x_92" tag:2];
    
    Chat * discover = [[Chat alloc] init];
    self.discover = discover ;
    [self addChildVc:self.discover title:@"聊天" image:@"Chat1" selectImage:@"Chat2" tag:3];
    
    MineVC *profile = [[MineVC alloc] init];
    self.profile = profile ;
    [self addChildVc:self.profile title:@"其他" image:@"Other" selectImage:@"Other2" tag:4];
    
}

#pragma mark -添加一个子控制器-
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage tag:(NSInteger)tag
{
    // 设置子控制器的文字和图片
    childVc.title = title;
    
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字样式
    NSMutableDictionary *textAttrs = [[NSMutableDictionary alloc] init];
    textAttrs[NSForegroundColorAttributeName] = RGB(123, 123, 123);
    NSMutableDictionary *selectTextAttrs = [[NSMutableDictionary alloc] init];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    childVc.tabBarItem.tag = tag;
    
    NSLog(@"tabBarItem.tag -- %ld",(long)childVc.tabBarItem.tag);
    
    PSNavigationVC * navi = [[PSNavigationVC alloc] initWithRootViewController:childVc];
    
    if ([title isEqualToString:@"麦霸"]) {
        navi.navigationBar.translucent = NO;
    }
    [self addChildViewController:navi];
    
}


- (void)addChildVC:(UIViewController*)chiledrenVC withTitle:(NSString*)title withSystemItem:(UITabBarSystemItem)item tag:(NSInteger)tag
{
    chiledrenVC.title = title;
    chiledrenVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:item tag:tag];
    PSNavigationVC * navi = [[PSNavigationVC alloc] initWithRootViewController:chiledrenVC];
    
    NSLog(@"%ld",(long)chiledrenVC.tabBarItem.tag);
    
    [self addChildViewController:navi];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
